<template>
	<div class="document-item">
		<el-card class="document-card" shadow="hover">
			<template #header>
				<div class="document-header">
					<span class="document-title">{{ document.title || '无标题' }}</span>
					<div class="document-actions">
						<el-button size="small" @click="toggleExpand">
							{{ isExpanded ? '收起' : '展开' }}
						</el-button>
						<el-button size="small" type="danger" @click="handleDelete">
							删除
						</el-button>
					</div>
				</div>
			</template>
			
			<div class="document-content">
				<div class="document-info">
					<div class="info-item">
						<span class="info-label">单据号：</span>
						<span class="info-value">{{ document.document_id || 'N/A' }}</span>
					</div>
					<div class="info-item">
						<span class="info-label">创建时间：</span>
						<span class="info-value">{{ formatDate(document.created_at) }}</span>
					</div>
					<div class="info-item">
						<span class="info-label">状态：</span>
						<el-tag :type="getStatusType(document.status)">
							{{ getStatusText(document.status) }}
						</el-tag>
					</div>
				</div>
				
				<div class="validation-summary">
					<div class="summary-item success">
						<span class="summary-label">成功：</span>
						<span class="summary-value">{{ successCount }}</span>
					</div>
					<div class="summary-item error">
						<span class="summary-label">失败：</span>
						<span class="summary-value">{{ errorCount }}</span>
					</div>
				</div>
				
				<div v-if="isExpanded" class="validation-details">
					<div v-if="validationResults.length === 0" class="no-results">
						暂无校验结果
					</div>
					<div v-else>
						<div v-for="(result, index) in validationResults" :key="index" class="validation-result">
							<div class="result-header">
								<span class="result-name">{{ result.name || '未知规则' }}</span>
								<el-tag :type="result.success ? 'success' : 'danger'" size="small">
									{{ result.success ? '通过' : '失败' }}
								</el-tag>
							</div>
							<div v-if="!result.success && result.message" class="result-message">
								{{ result.message }}
							</div>
						</div>
					</div>
				</div>
			</div>
		</el-card>
	</div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue';

// 定义props
const props = defineProps({
	document: {
		type: Object,
		required: true
	}
});

// 定义emits
const emit = defineEmits(['delete']);

// 响应式数据
const isExpanded = ref(false);

// 计算属性
const validationResults = computed(() => {
	if (!props.document.validation_results) return [];
	
	// 处理不同格式的校验结果
	if (typeof props.document.validation_results === 'string') {
		try {
			return JSON.parse(props.document.validation_results);
		} catch (e) {
			// 如果不是JSON格式，尝试按行分割
			return props.document.validation_results.split('\n').filter((line: string) => line.trim());
		}
	}
	
	return Array.isArray(props.document.validation_results) 
		? props.document.validation_results 
		: [];
});

const successCount = computed(() => {
	return validationResults.value.filter((result: any) => {
		if (typeof result === 'object') {
			return result.success === true || result.status === 'success';
		}
		return false;
	}).length;
});

const errorCount = computed(() => {
	return validationResults.value.filter((result: any) => {
		if (typeof result === 'object') {
			return result.success === false || result.status === 'error';
		}
		return false;
	}).length;
});

// 方法
const toggleExpand = () => {
	isExpanded.value = !isExpanded.value;
};

const handleDelete = () => {
	emit('delete', props.document.id);
};

const formatDate = (dateString: string) => {
	if (!dateString) return 'N/A';
	
	try {
		const date = new Date(dateString);
		return date.toLocaleString();
	} catch (e) {
		return dateString;
	}
};

const getStatusType = (status: string) => {
	switch (status) {
		case 'completed':
			return 'success';
		case 'processing':
			return 'warning';
		case 'failed':
			return 'danger';
		default:
			return 'info';
	}
};

const getStatusText = (status: string) => {
	switch (status) {
		case 'completed':
			return '已完成';
		case 'processing':
			return '处理中';
		case 'failed':
			return '失败';
		default:
			return '未知';
	}
};
</script>

<style scoped>
.document-item {
	margin-bottom: 15px;
}

.document-card {
	border-radius: 8px;
}

.document-header {
	display: flex;
	justify-content: space-between;
	align-items: center;
}

.document-title {
	font-weight: bold;
	font-size: 16px;
}

.document-actions {
	display: flex;
	gap: 5px;
}

.document-content {
	padding: 10px 0;
}

.document-info {
	display: flex;
	flex-wrap: wrap;
	gap: 15px;
	margin-bottom: 15px;
}

.info-item {
	display: flex;
	align-items: center;
}

.info-label {
	font-weight: bold;
	margin-right: 5px;
	color: #606266;
}

.info-value {
	color: #303133;
}

.validation-summary {
	display: flex;
	gap: 20px;
	margin-bottom: 15px;
}

.summary-item {
	display: flex;
	align-items: center;
}

.summary-label {
	font-weight: bold;
	margin-right: 5px;
}

.summary-value {
	font-weight: bold;
	font-size: 16px;
}

.success .summary-value {
	color: #67C23A;
}

.error .summary-value {
	color: #F56C6C;
}

.validation-details {
	border-top: 1px solid #EBEEF5;
	padding-top: 15px;
}

.validation-result {
	margin-bottom: 10px;
	padding: 10px;
	background-color: #F5F7FA;
	border-radius: 4px;
}

.result-header {
	display: flex;
	justify-content: space-between;
	align-items: center;
	margin-bottom: 5px;
}

.result-name {
	font-weight: bold;
}

.result-message {
	color: #F56C6C;
	font-size: 14px;
}

.no-results {
	text-align: center;
	padding: 10px;
	color: #909399;
}
</style>